function clearSelect(sid) {
	if (sid == "parentCode") {
		$("#" + sid).empty().append("<option value='-1'>--选择一级代理--</option>");
	} else if(sid=="sonCode") {
		$("#" + sid).empty().append("<option value='-1'>--选择二级代理--</option>");
	} 
}
// 进行遍历存值
function fillSelect_sort(sid, address) {

	if (!address || !sid) {
		return;
	}
	var $select = $("#" + sid);
	$select.empty();
	
	if (address.length >= 1) {
		if (sid == "parentCode" || sid == "areaCode") {
			$select.append("<option value='-1'>--选择一级代理--</option>");
		} else if (sid == "sonCode") {
			$select.append("<option value='-1'>--选择二级代理--</option>");
		}
	}else{
		if (sid == "parentCode" || sid == "areaCode") {
			$select.append("<option value='-1'>--无下级代理--</option>");
		} else if (sid == "sonCode") {
			$select.append("<option value='-1'>--无下级代理--</option>");
		}
	}
	 
	var proCode = $("#henParentCode").val();
	var cityCode = $("#henSonCode").val(); 
	for (var i = 0; i < address.length; i++) {
		$select.append("<option value='" + address[i].CODE + "'>"+ address[i].NAME + "</option>");
		if (proCode == address[i].CODE) {
			$("#" + sid).find("option[value=" + proCode + "]").attr("selected",true);
		}
		if (cityCode == address[i].CODE) {
			$("#" + sid).find("option[value=" + cityCode + "]").attr("selected", true);
		}
		 
	}

}
//load one 
function loadProvince(id) { 
	clearSelect("parentCode");
	MccQuery.loadSonOper(id,function(objs) {
		clearSelect("sonCode");
		fillSelect_sort("parentCode", objs);
		loadCity();
	});
}
function loadCity() { 
 
	var	sortCode=$("#parentCode").val();
	if (sortCode==null || sortCode=="-1") {
		return;
	}
	MccQuery.loadSonOper(sortCode, function(sortSmall) {
		fillSelect_sort("sonCode", sortSmall);
	});
}

function loadOneAgent() { 
 
	var	sortCode=$("#areaCode").val();
/*	if (sortCode==null || sortCode=="-1") {
		return;
	}*/
	MccQuery.loadOneAgent(sortCode, function(sortSmall) {
		fillSelect_sort("sonCode", sortSmall);
	});
}

var profitRuleTH = '<tr class="tb-line">' + 
						'<td class="tb-td"  > <div class="tb-h4">成本扣率</div> </td>' + 
						'<td class="tb-td"  > <div class="tb-h4">借记卡成本扣率</div> </td>' + 
						'<td class="tb-td"  > <div class="tb-h4">贷记卡成本扣率</div> </td>' + 
						'<td class="tb-td"  > <div class="tb-h4">借记卡封顶成本金额</div> </td>' + 
						'<td class="tb-td"  > <div class="tb-h4">贷记卡最低签扣</div> </td>' + 
					'</tr>';
var profitRuleTD = '<tr class="tb-line">' + 
						'<td class="tb-td" >t_mccTypeName</td>' + 
						'<td class="tb-td" >t_profitFee%</td>' + 
						'<td class="tb-td" >t_profitSettleRate%</td>' + 
						'<td class="tb-td" >t_maxFee元</td>' + 
						'<td class="tb-td" >t_merchantMinFee%</td>' + 
					'</tr>';

var profitPerTH = '<tr class="tb-line">' + 
					'<td class="tb-td"  > <div class="tb-h4">区间最小金额</div> </td>' + 
					'<td class="tb-td"  > <div class="tb-h4">区间最大金额</div> </td>' + 
					'<td class="tb-td"  > <div class="tb-h4">代理商分润比例</div> </td>' + 
				'</tr>' ;
  
var profitPerTD = '<tr class="tb-line">' + 
					'<td class="tb-td" >t_limit_amt元</td>' + 
					'<td class="tb-td" >t_max_amt元</td>' + 
					'<td class="tb-td" >t_percent%</td>' + 
				'</tr>' ;

//加载默认分润模板信息
function loadDefProfitModule() {
	
	MccQuery.queryDefProfitModelInfo( function(sortSmall) {
		fillDefProfitModuleInfo(sortSmall);
		
		$('#tabBody').hide();
		$('#defProfitModuleDiv').show();
	});
}
function fillDefProfitModuleInfo(sortSmall) {
	var ruleMap = sortSmall[0];
	var proMap = sortSmall[1];

	var $defRuleTable = $("#defRuleTable");
	$defRuleTable.empty();
			
	$("#defRuleTable" ).empty().append( profitRuleTH );
	if( ruleMap != undefined ){
		for(var s = 0; s < ruleMap.length ; s++){
			var rule = ruleMap[s];
			var profitRuleStr = profitRuleTD;
			
			profitRuleStr = profitRuleStr.replace("t_mccTypeName",rule.mccTypeName );
			profitRuleStr = profitRuleStr.replace("t_profitFee",rule.profitFee );
			profitRuleStr = profitRuleStr.replace("t_profitSettleRate",rule.profitSettleRate );
			profitRuleStr = profitRuleStr.replace("t_maxFee",(rule.maxFee == 0 ? "" : rule.maxFee) );
			profitRuleStr = profitRuleStr.replace("t_merchantMinFee",rule.merchantMinFee );
			
			$defRuleTable.append( profitRuleStr );
		}
	}

	var $defProTable = $("#defProTable" );
	$defProTable.empty();
			
	$("#defProTable").empty().append( profitPerTH );
	if( proMap != undefined ){
		for(var s = 0; s < proMap.length ; s++){
			var pro = proMap[s];
			var profitPerStr = profitPerTD;
			
			profitPerStr = profitPerStr.replace("t_limit_amt",pro.limitAmt );
			profitPerStr = profitPerStr.replace("t_max_amt",( ( null == pro.maxAmt ) ? "" : pro.maxAmt ) );
			profitPerStr = profitPerStr.replace("t_percent",pro.percent );

			$defProTable.append( profitPerStr );
		}
	}
}

function getFeeName( feeCode ) {
	var feeName = "";
	if( null != feeCode && "" != feeCode && feeCode == "01" ){
		feeName = "标准类";
	}
	if( null != feeCode && "" != feeCode && feeCode == "02" ){
		feeName = "优惠类";
	}
	if( null != feeCode && "" != feeCode && feeCode == "03" ){
		feeName = "特殊计费类";
	}
	if( null != feeCode && "" != feeCode && feeCode == "04" ){
		feeName = "D0交易";
	}
	if( null != feeCode && "" != feeCode && feeCode == "05" ){
		feeName = "云闪付";
	}
	if( null != feeCode && "" != feeCode && feeCode == "06" ){
		feeName = "银联扫码(非优惠)";
	}
	if( null != feeCode && "" != feeCode && feeCode == "07" ){
		feeName = "银联扫码(优惠)";
	}
	if( null != feeCode && "" != feeCode && feeCode == "08" ){
		feeName = "支付宝银联扫码";
	}
	return feeName;
}

function autoGetAgentInfoByName() {
	 var url=window.location.host; 
	$( "#showOneAgentName" ).autocomplete({// showOneAgentName 输入框id
        source: function( request, response ) {
            $.ajax({
				type:'post',
        		url:"http://"+url+"/fmposm/terminalSnBatchLeading/queryRuleName.ct",
                dataType: "json",
                data:{
                    "posInfo.agentName": request.term    // 获取输入框内容
                },
    	  		success: function( data ){

                      response( $.map( data, function( item ) { // 此处是将返回数据转换为 JSON对象，并给每个下拉项补充对应参数
                        return {
                             // 设置item信息
                             label: item.accountNo + "-" + item.realName , // 下拉项显示内容
                             value: item.realName ,   // 下拉项对应数值
                             ids: item.id ,   // 下拉项对应数值
                             code: item.accountNo //, // 其他参数， 可以自定义
                        }
                    }));  
    	  		},error:function(rsg){
    	  			alert("查询代理商名称失败！");
    	  		}
            });
        },
        minLength: 1,  // 输入框字符个等于2时开始查询
        select: function( event, ui ) { // 选中某项时执行的操作
            // 存放选中选项的信息
			$("#showOneAgentId").val( ui.item.ids );
			$("#showOneAgentName").val( ui.item.label );
		//	$("#showOneAgentAccount").val( ui.item.code );
         }
        });
}

